package com.stock.project.system.warehouse.controller;

import com.stock.common.utils.poi.ExcelUtil;
import com.stock.framework.aspectj.lang.annotation.Log;
import com.stock.framework.aspectj.lang.enums.BusinessType;
import com.stock.framework.web.controller.BaseController;
import com.stock.framework.web.domain.AjaxResult;
import com.stock.framework.web.page.TableDataInfo;
import com.stock.project.system.warehouse.domain.Warehouse;
import com.stock.project.system.warehouse.service.IWarehouseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 仓库 信息操作处理
 *
 * @author stock
 * @date 2019-03-14
 */
@Controller
@RequestMapping("/system/warehouse")
public class WarehouseController extends BaseController {
    private String prefix = "system/warehouse";

    @Autowired
    private IWarehouseService warehouseService;

    @RequiresPermissions("system:warehouse:view")
    @GetMapping()
    public String warehouse() {
        return prefix + "/warehouse";
    }

    /**
     * 查询仓库列表
     */
    @RequiresPermissions("system:warehouse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Warehouse warehouse) {
        startPage();
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        return getDataTable(list);
    }


    /**
     * 导出仓库列表
     */
    @RequiresPermissions("system:warehouse:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Warehouse warehouse) {
        List<Warehouse> list = warehouseService.selectWarehouseList(warehouse);
        ExcelUtil<Warehouse> util = new ExcelUtil<Warehouse>(Warehouse.class);
        return util.exportExcel(list, "warehouse");
    }

    /**
     * 新增仓库
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存仓库
     */
    @RequiresPermissions("system:warehouse:add")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Warehouse warehouse) {
        return toAjax(warehouseService.insertWarehouse(warehouse));
    }

    /**
     * 修改仓库
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Warehouse warehouse = warehouseService.selectWarehouseById(id);
        mmap.put("warehouse", warehouse);
        return prefix + "/edit";
    }

    /**
     * 修改保存仓库
     */
    @RequiresPermissions("system:warehouse:edit")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Warehouse warehouse) {
        return toAjax(warehouseService.updateWarehouse(warehouse));
    }

    /**
     * 删除仓库
     */
    @RequiresPermissions("system:warehouse:remove")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(warehouseService.deleteWarehouseByIds(ids));
    }

}
